﻿using MediatR;
using SmartApplication.Api.Applient.Command.Kill;
using SmartApplication.Api.Dto;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;
using System.Threading.Tasks.Dataflow;

namespace SmartApplication.Api.Applient.CommandHandler.Kill
{
    public class ListKillHandler : IRequestHandler<ListKillCommand, ApiResult<List<KillDto>>>
    {
        public ListKillHandler(IBaseRepository<KillModel> killRepository,IBaseRepository<BusinessModel> businessRepository)
        {
            KillRepository = killRepository;
            BusinessRepository = businessRepository;
        }

        public IBaseRepository<KillModel> KillRepository { get; }
        public IBaseRepository<BusinessModel> BusinessRepository { get; }

        /// <summary>
        /// 显示屠宰信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<KillDto>>> Handle(ListKillCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<KillDto>>();

            var kill = KillRepository.GetAll().Where(x => x.SourceId == request.Id);
            var bus=BusinessRepository.GetAll();

            var list = from k in kill
                       join b in bus on k.BusinessId equals b.Id
                       select new KillDto()
                       {
                           Id = k.Id,
                           BeginTime = k.BeginTime,
                           BusinessAdd = b.BusinessAdd,
                           BusinessId = b.Id,
                           BusinessName = b.BusinessName,
                           BusinessPic = b.BusinessPic,
                           CreateDate = b.CreateDate,
                           EndTime = k.EndTime,
                           Height = k.Height,
                           Introduction = b.Introduction,
                           KillPic = k.KillPic,
                           SourceId = k.SourceId
                       };
            res.Code = EnumCode.Success;
            res.Message = "显示屠宰信息成功";
            res.Data=list.ToList();
            return res;
        }
    }
}
